#
# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
#
# SPDX-License-Identifier: BSD-2-Clause
#

default: AutoCorresTest

all: AutoCorres AutoCorresTest AutoCorresSEL4 AutoCorresDoc

report-regression:
	@echo AutoCorres AutoCorresTest AutoCorresDoc AutoCorresSEL4

PARSE_TESTS_C := $(wildcard tests/parse-tests/*.c)
PARSE_TESTS_THY := $(patsubst %.c,%.thy,$(PARSE_TESTS_C))

PROOF_TESTS_C := $(wildcard tests/proof-tests/*.c)
PROOF_TESTS_THY := $(wildcard tests/proof-tests/*.thy)

EXAMPLES_C := $(wildcard tests/examples/*.c)
EXAMPLES_THY := $(wildcard tests/examples/*.thy)

ALL_TESTS_C := $(PARSE_TESTS_C) $(PROOF_TESTS_C) $(EXAMPLES_C)
ALL_TESTS_THY := $(PARSE_TESTS_THY) $(PROOF_TESTS_THY) $(EXAMPLES_THY)

# Generate an Isabelle "ROOT" file containing all of our test cases.
tests/ROOT: $(ALL_TESTS_C) $(ALL_TESTS_THY) ../../misc/scripts/gen_isabelle_root.py
	python3 ../../misc/scripts/gen_isabelle_root.py -o $@ -b AutoCorres -s AutoCorresTest -d HOL-Number_Theory \
		-i tests/parse-tests -i tests/proof-tests -i tests/examples --quick-and-dirty --dir parse-tests --dir proof-tests --dir examples

# Generate a Isabelle "All.thy" file containing imports to all the test cases.
tests/All.thy: $(ALL_TESTS_C) $(ALL_TESTS_THY) ../../misc/scripts/gen_isabelle_root.py
	python3 ../../misc/scripts/gen_isabelle_root.py -T -o $@ \
		-i tests/parse-tests -i tests/proof-tests -i tests/examples

SPDX_TAG=SPDX-License-Identifier

# Generate a template ".thy" file from a ".c" file,
# but only for ".c" files in tests/parse-tests.
$(PARSE_TESTS_THY): %.thy: %.c
	@echo "Generating '$@' from '$<'..."
	@printf '(*\n * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)\n *\n' > $@
	@printf ' * $(SPDX_TAG): BSD-2-Clause\n *)\n\n' >> $@
	@printf '(* This file was generated by tools/autocorres/Makefile *)\n\n' >> $@
	@printf 'theory %s\nimports "AutoCorres.AutoCorres"\nbegin\n\n' $(notdir $(basename $<)) >> $@
	@printf 'external_file "%s"\n\n' $(notdir $<) >> $@
	@printf 'install_C_file "%s"\n\n' $(notdir $<) >> $@
	@printf 'autocorres "%s"\n\n' $(notdir $<) >> $@
	@printf 'end\n' >> $@

# Test all the files in our "tests/" directory.
AutoCorresTest: clean-tests tests/ROOT
	$(ISABELLE_TOOL) build -d ../.. -d tests -v AutoCorresTest
.PHONY: AutoCorresTest

# Parse the seL4 kernel
AutoCorresSEL4:
	make -C ../../proof CBaseRefine
	$(ISABELLE_TOOL) build -d ../.. -v AutoCorresSEL4
.PHONY: AutoCorresSEL4

# Build the documentation sub-sessions.
AutoCorresDoc:
	$(ISABELLE_TOOL) build -d ../.. -D doc/quickstart

# Clean out files generated by the test suite.
clean-tests:
	rm -f tests/parse-tests/*.thy
	rm -f tests/ROOT
.PHONY: clean-tests

clean: clean-tests

HEAPS := AutoCorres

include ../../misc/isa-common.mk

